package tw.twandroidproj.app.ui.resume;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tw.twandroidproj.app.R;

public class Resume_school_check extends Activity {

    private String mCurrentProviceName="";

    private JSONObject mJsonObj;

    private String[] SchoolDatas=new String[]{""};

    private String[] Schoolchecks=new String[]{""};

    private Map<String,String[]>SchoolNameMap=new HashMap<String,String[]>();

    ListView list_resume_check_school;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resume_school_check);

        initJsonData();
        initDatas();

        List<Map<String,Object>>listItems=new ArrayList<Map<String,Object>>();
        for (int i=0;i<SchoolDatas.length;i++)
        {
            Map<String,Object>listItem=new HashMap<String,Object>();
            listItem.put("province",SchoolDatas[i]);
            listItems.add(listItem);
        }
        SimpleAdapter simpleAdapter=new SimpleAdapter(Resume_school_check.this,listItems,R.layout.resume_school_check_listitem,
                new String[]{"province"},
                new int[]{R.id.school_province}
                );

        ListView list_resume_check_area=(ListView)findViewById(R.id.resume_school_check_area);
        list_resume_check_school=(ListView)findViewById(R.id.resume_school_check_school);
        list_resume_check_area.setAdapter(simpleAdapter);

        list_resume_check_area.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                mCurrentProviceName=SchoolDatas[position];
                String[] schoolnames=SchoolNameMap.get(mCurrentProviceName);
                Schoolchecks=SchoolNameMap.get(mCurrentProviceName);
                if (schoolnames==null)
                {
                    schoolnames=new String[] { "" };
                }

                List<Map<String,Object>>listItems=new ArrayList<Map<String,Object>>();
                for (int i=0;i<SchoolDatas.length;i++)
                {
                    Map<String,Object>listItem=new HashMap<String,Object>();
                    listItem.put("school",schoolnames[i]);
                    listItems.add(listItem);
                }

                SimpleAdapter simpleAdapter=new SimpleAdapter(Resume_school_check.this,listItems,R.layout.resume_school_check_listitem,
                        new String[]{"school"},
                        new int[]{R.id.school_province}
                );

                list_resume_check_school.setAdapter(simpleAdapter);
            }
        });

        list_resume_check_school.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle=new Bundle();
                bundle.putString("schoolname", Schoolchecks[position]);
                Intent intent =new Intent(Resume_school_check.this,Resume_edit.class);
                intent.putExtras(bundle);
                Resume_school_check.this.setResult(2, intent);

                finish();

            }
        });

    }

    private void initJsonData()
    {
        try
        {
            StringBuffer school_data=new StringBuffer();
            InputStream data=getAssets().open("school_name.json");
            int len = -1;
            byte[] buf=new byte[data.available()];
            while ((len=data.read(buf))!=-1)
            {
                school_data.append(new String(buf,0,len));
            }
            data.close();
            mJsonObj=new JSONObject(school_data.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void initDatas()
    {
        try
        {
            JSONArray jsonArray=mJsonObj.getJSONArray("schoollist");
            SchoolDatas=new String[jsonArray.length()];
            for(int i=0;i<jsonArray.length();i++)
            {
                JSONObject jsonP=jsonArray.getJSONObject(i);
                String province=jsonP.getString("p");

                SchoolDatas[i]=province;

                JSONArray jsonS=null;
                try
                {
                    jsonS=jsonP.getJSONArray("s");
                }catch (Exception e1)
                {
                    continue;
                }
                String[] schoolname=new String[jsonS.length()];
                for(int j=0;j<jsonS.length();j++)
                {
                    JSONObject jsonschoolname=jsonS.getJSONObject(j);
                    String school_name=jsonschoolname.getString("u");
                    schoolname[j]=school_name;
                }
                SchoolNameMap.put(province,schoolname);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        mJsonObj=null;
    }

}
